<template>
  <div>
    <div class="outmain ba_f">
      <div class="shuju_title mar_b20">
        <div class="shuju_title_text">
          <span>支付设置</span>
        </div>
      </div>
      <el-form ref="form" :model="form" label-width="280px">
        <div class="ba_f59 pad_20 flex-bet">
          <div>
            <div class="wei fon_14">微信支付</div>
            <div class="mar_t10">
              微信普通支付--已申请微信普通支付账号，且保证和当前小程序或公众号已保持关联，微信商户登录地址：
              <a href="https://pay.weixin.qq.com/" target="_blank">https://pay.weixin.qq.com/</a>
            </div>
            <div class="">
              微信服务商支付--确保当前主体已开通微信服务商支付，且保证和当前小程序或公众号已保持关联，微信服务商登录地址：
              <a href="https://pay.weixin.qq.com/partner/public/home" target="_blank">https://pay.weixin.qq.com/partner/public/home</a>
            </div>
          </div>
          <div>
            <el-switch v-model="form.payOpen" active-value="1" inactive-value="2"></el-switch>
          </div>
        </div>
        <div v-if="form.payOpen=='1'" class="mar_t20">
          <el-form-item label="微信支付类型">
            <el-radio-group v-model="form.wxPayType">
              <el-radio label="1">微信普通支付</el-radio>
              <el-radio label="2">微信服务商支付</el-radio>
            </el-radio-group>
          </el-form-item>
          <template v-if="form.payOpen=='1' && form.wxPayType=='1'">
            <el-form-item label="请输入微信支付商户号" :required="true">
              <el-input v-model="form.mchId" placeholder="请输入微信支付商户号"></el-input>
            </el-form-item>
            <el-form-item label="请输入微信支付商户秘钥" :required="true">
              <el-input v-model="form.key" type="password" placeholder="请输入微信支付商户秘钥"></el-input>
            </el-form-item>
            <el-form-item label="微信支付证书apiclient_cert.pem" :required="true">
              <el-input v-model="form.apiclient_cert" type="textarea" :rows="6" placeholder="请输入微信支付证书："></el-input>
              <div v-if="form.isCertfile=='1'" class="subjectcolor2">已上传</div>
            </el-form-item>
            <el-form-item label="微信支付证书apiclient_key.pem" :required="true">
              <el-input v-model="form.apiclient_key" type="textarea" :rows="6" placeholder="请输入微信支付证书："></el-input>
              <div class="subjectcolor2" v-if="form.iskeyfile=='1'">已上传</div>
            </el-form-item>
          </template>
        </div>
<!--        <div class="ba_f59 pad_20 flex-bet mar_t20">-->
<!--          <div>-->
<!--            <div class="wei fon_14">微信服务商支付</div>-->
<!--            <div class="mar_t10">-->
<!--              确保当前主体已开通微信服务商支付，且保证和当前小程序或公众号已保持关联，微信服务商登录地址：-->
<!--              <a href="https://pay.weixin.qq.com/partner/public/home" target="_blank">https://pay.weixin.qq.com/partner/public/home</a>-->
<!--            </div>-->
<!--          </div>-->
<!--          <div>-->
<!--            <el-switch v-model="form.servicePayOpen" active-value="1" inactive-value="2"></el-switch>-->
<!--          </div>-->
<!--        </div>-->
        <div v-if="form.payOpen=='1' && form.wxPayType=='2'" class="mar_t20">
          <el-form-item label="公众号appId" :required="true">
            <el-input v-model="form.serviceAppId" placeholder="请输入公众号appId"></el-input>
          </el-form-item>
          <el-form-item label="请输入服务商商户号" :required="true">
            <el-input v-model="form.serviceMchId" placeholder="请输入服务商商户号"></el-input>
            <div class="yb_margin lh16">需要填写申请为服务商的商户号。注：服务商的商户号与微信支付的商户号不是同一个号。</div>
          </el-form-item>
          <el-form-item label="请输入服务商商户秘钥" :required="true">
            <el-input v-model="form.serviceKey" type="password" placeholder="请输入服务商商户秘钥"></el-input>
            <div class="yb_margin lh16">此商户密钥为服务商商户号对应的支付密钥，与微信支付的支付密钥不相同</div>
          </el-form-item>
          <el-form-item label="平台子商户号：" :required="true">
            <el-input v-model="form.sonMchId" placeholder="请输入平台子商户号"></el-input>
          </el-form-item>
          <el-form-item label="服务商证书apiclient_cert.pem" :required="true">
            <el-input v-model="form.service_apiclient_cert" type="textarea" :rows="6" placeholder="请输入服务商证书"></el-input>
            <div v-if="form.isServiceCertfile=='1'" class="subjectcolor2">已上传</div>
          </el-form-item>
          <el-form-item label="服务商证书apiclient_key.pem：" :required="true">
            <el-input v-model="form.service_apiclient_key" type="textarea" :rows="6" placeholder="请输入服务商证书："></el-input>
            <div v-if="form.isServiceKeyfile=='1'" class="subjectcolor2">已上传</div>
          </el-form-item>
          <el-form-item label="扫码点餐" :required="true">
            <el-switch v-model="form.smOpen" active-value="1" inactive-value="2"></el-switch>
          </el-form-item>
          <template v-if="form.smOpen=='1'">
            <el-form-item label="服务商证书序号" :required="true">
              <el-input v-model="form.servicePayNo" placeholder="请输入服务商证书序号"></el-input>
            </el-form-item>
            <el-form-item label="服务商apiV3密钥" :required="true">
              <el-input v-model="form.serviceV3Key" type="password" placeholder="请输入服务商apiV3密钥"></el-input>
            </el-form-item>
          </template>
        </div>
        <div class="ba_f59 pad_20 flex-bet mar_t20">
          <div>
            <div class="wei fon_14">余额支付</div>
            <div class="mar_t10">用户可预先充值金额来进行支付的一种渠道</div>
          </div>
          <div>
            <el-switch v-model="form.balancePayOpen" active-value="1" inactive-value="2"></el-switch>
          </div>
        </div>
        <div class="ba_f59 pad_20 flex-bet mar_t20">
          <div class>
            <div class="wei fon_14">找人代付</div>
            <div class="mar_t10">启用代付功能后，代付发起人（买家）下单后， 可将订单分享给小伙伴，请他帮忙付款</div>
          </div>
          <div>
            <el-switch v-model="form.payMentOpen" active-value="1" inactive-value="2"></el-switch>
          </div>
        </div>
        <div class="mar_t20" v-if="form.payMentOpen=='1'">
          <el-form-item label="发起人的求助" :required="true">
            <el-input v-model="form.help" placeholder="请输入发起人的求助信息"></el-input>
            <div class="yb_margin lh16">分享给小伙伴的求助信息，如："特别喜欢你英姿飒爽的样子，超酷的~"</div>
          </el-form-item>
          <el-form-item label="代付背景图片" :required="true">
            <div class="uploadSource bor_de rel flex curs" v-if="form.icon">
              <label class="statusLabel curs" @mouseenter="enter(1)" @mouseleave="leave(1)">
                <i class="el-icon-check" ref="imgicon1"></i>
                <i class="el-icon-close" @click="deleteMultiImage(form.icon,1)"></i>
              </label>
              <img :src="form.icon" @click="test(1)" class="wid" />
            </div>
            <div class="uploadSource flex-center curs" v-else @click="test(1)">
              <i class="el-icon-plus fon_40 color_d9"></i>
            </div>
            <div class="yb_margin lh16">建议尺寸：750x370像素</div>
          </el-form-item>
        </div>
        <div class="ba_f59 pad_20 flex-bet mar_t20">
          <div>
            <div class="wei fon_14">货到付款</div>
            <div class="mar_t10">用户可选择收到货后再付款</div>
          </div>
          <div>
            <el-switch v-model="form.deliveryOpen" active-value="1" inactive-value="2"></el-switch>
          </div>
        </div>
      </el-form>
    </div>
    <div class="mar_t20 ba_f pad_20 t_c">
      <el-button type="primary" @click="submitForm">保存</el-button>
    </div>
    <attachment-img ref="img1" type="img" @attachment="imgadd1"></attachment-img>
  </div>
</template>
<script>
  import attachmentImg from '@/plugins/attachment-img';
import { getConfig, postConfig } from '@/api/setup';

export default {
  components: { attachmentImg },
  created() {
    this.bus.$emit('loading', true);
    this.init();
  },
  data() {
    return {
      form: {
        wxPayType: '1',
        payOpen: '1',
        balancePayOpen: '2',
        // servicePayOpen: '2',
        payMentOpen: '2',
        deliveryOpen: '2',
        help: '',
        icon: '',
        mchId: '',
        key: '',
        apiclient_cert: '',
        isCertfile: '',
        apiclient_key: '',
        iskeyfile: '',
        serviceAppId: '',
        serviceMchId: '',
        serviceKey: '',
        sonMchId: '',
        service_apiclient_cert: '',
        isServiceCertfile: '',
        service_apiclient_key: '',
        isServiceKeyfile: '',
        smOpen: '2',
        servicePayNo: '',
        serviceV3Key: '',
      }
    };
  },
  methods: {
    enter(i = 0) {
      this.$refs['imgicon' + i].style.display = 'none';
    },
    leave(i = 0) {
      this.$refs['imgicon' + i].style.display = 'inline-block';
    },
    test(i = 0) {
      // console.log(this.$refs, i)
      this.$refs['img' + i].dialogVisible = true;
    },
    imgadd1(url) {
      this.form.icon = url.str;
    },
    deleteMultiImage(item, index, type) {
      Array.prototype.remove = function (val) {
        var index = this.indexOf(val);
        if (index > -1) {
          this.splice(index, 1);
        }
      };
      if (type == 'multigraph') {
        switch (index) {
          case 5:
            break;
        }
      } else {
        switch (index) {
          case 1:
            this.form.icon = '';
            break;
        }
      }
    },
    async init() {
      const { data } = await getConfig({ ident: 'payConfig' });
      Object.assign(this.form, data);
      if (data.isCertfile == '1') {
        this.form.apiclient_cert = '';
      }
      if (data.iskeyfile == '1') {
        this.form.apiclient_key = '';
      }
      if (data.isServiceCertfile == '1') {
        this.form.service_apiclient_cert = '';
      }
      if (data.isServiceKeyfile == '1') {
        this.form.service_apiclient_key = '';
      }
      this.bus.$emit('loading', false);
    },
    submitForm() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          console.log(this.form);
          this.form.ident = 'payConfig';
          // return
          const { msg } = await postConfig(this.form);
          this.$baseMessage(msg, 'success');
          this.init();
        } else {
          return false;
        }
      });
    }
  }
};
</script>
